﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Karel.Pages
{
    /// <summary>
    /// Dictionary content download page
    /// </summary>
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Returns the cache content
        /// </summary>
        protected string GetContentFromCache() 
        {
            object objValue = HttpContext.Current.Cache["Content"];
            if (objValue == null)
                return string.Empty;
            return objValue.ToString();
        }

        protected string GetFilename()
        {
            object objValue = HttpContext.Current.Cache["Filename"];
            if (objValue == null)
                return "slovnik.txt";
            return objValue.ToString();
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // download the content
            Response.Clear();
            Response.ContentType = "text/plain";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + GetFilename() + "\"");
            Response.Write(GetContentFromCache());
            Response.Flush();
            Response.End();
        }
    }
}